import Express from "express";
import path from 'path'
import multer from "multer";
import UploadControll from "../controllers/uploadController";
// 创建路由
const Router = Express.Router()
// 文件上传配置
const uploader = multer({
  storage: multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, path.join(__dirname, '../../uploads'))
    },
    filename: function (req, file, cb) {
      const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
      cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname)); // 指定文件名
    }
  }), limits: {
    fileSize: 1024 * 1024 // 1M
  }
})

Router.post('/', uploader.single('avatar'), UploadControll.uploadHandler)
Router.post('/files', uploader.array('photos', 3), UploadControll.uploadHandler)

export default Router